package yxy.game.pm2.module.playerCommand;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.BaseGameRedisLockBean;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

/**
 * @author wrs
 * @since 2022-08-04
 * @descrition 统御
 * */
public class PlayerCommandModule extends BaseGameRedisLockBean {

    // bean===========================================================================
    private int point; //点数
    private int[] propertyLevel = new int[3]; //攻防血属性点数，初始为零
    private int resetTimes; //重置次数
    private DateTime updateTime; //更新时间，记录重置次数的更新时间

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }

    public int[] getPropertyLevel() {
        return propertyLevel;
    }

    public int getPropertyLevel(int index){
        return propertyLevel[index];
    }

    public void setPropertyLevel(int[] propertyLevel) {
        this.propertyLevel = propertyLevel;
    }

    public int setPropertyLevel(int index,int value){
        int oldValue = propertyLevel[index];
        propertyLevel[index] = value;
        return oldValue;
    }

    public int getResetTimes() {
        return resetTimes;
    }

    public void setResetTimes(int resetTimes) {
        this.resetTimes = resetTimes;
    }

    public DateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(DateTime updateTime) {
        this.updateTime = updateTime;
    }

    // func===========================================================================

    private static PlayerCommandModule create(String playerCode) {
        PlayerBean playerBean = PlayerBean.get(playerCode);

        PlayerCommandModule module = new PlayerCommandModule();
        if (playerBean.getLevel() == 0){
            module.setPoint(1); //新号创建等级为0
        }else {
            module.setPoint(playerBean.getLevel()); //初始点数为玩家当前等级
        }
        module.setResetTimes(0); //初始重置次数为0
        module.setPropertyLevel(new int[3]);
        module.setUpdateTime(DateTime.now()); //重置次数的更新时间

        module.set(playerCode);
        return module;
    }

    private void update(String playerCode){
        //todo 每月1号0时  重置次数改为初始值 0
        int lastUpdateTime = getUpdateTime().getMonthOfYear();//上一次更新时间
        if (lastUpdateTime != DateTime.now().getMonthOfYear()){
            setResetTimes(0);
            setUpdateTime(DateTime.now());
            set(playerCode);
        }
    }

    // redis==========================================================================

    static public String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:PLAYER_COMMAND", playerCode);
    }

    static public PlayerCommandModule get(String playerCode) {

        try {
            PlayerCommandModule module = getv(key(playerCode));
            //todo 后期放数据库
            if (module == null) {
                module = MySQLCacheDAO.getV(key(playerCode), PlayerCommandModule.class);
                if (module != null) module.set(playerCode);
            }

            if (module == null) {
                module = create(playerCode);
            }
            module.update(playerCode);
            return module;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public void set(String playerCode) {
        try {
            setv(key(playerCode),this);
            check(playerCode);
        }catch (RedisException e){
            e.printStackTrace();
        }
    }

    //红点
    private void check(String playerCode) {
        if (getPoint() > 0){
            new TipHandle().updateTip(playerCode, TipType.主公_统御切页,true);
        }else {
            new TipHandle().updateTip(playerCode,TipType.主公_统御切页,false);
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                PlayerCommandModule module = PlayerCommandModule.get(playerCode);
                MySQLCacheDAO.setV(key, module);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // lock==========================================================================

    static private String lockKey(String playerCode) {
        return String.format("lock:%s", key(playerCode));
    }

    static public boolean lockPlayerCommandModule(String playerCode) throws InterruptedException {
        return tryLock(lockKey(playerCode));
    }

    static public void unlockPlayerCommandModule(String playerCode) {
        unlock(lockKey(playerCode));
    }
}
